package com.tc.controller;

import com.tc.domain.Drug_reimbursement_ratio;
import com.tc.dto.Drug_reimbursement_ratioDTO;
import com.tc.dto.Drug_reimbursement_ratioQueryDTO;
import com.tc.service.IDrug_reimbursement_ratioService;
import com.tc.utils.ResultVo;
import com.tc.vo.Drug_reimbursement_ratioVO;
import com.tc.vo.PageResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tc
 * @since 2025-07-09
 */
@RestController
@RequestMapping("/drug_reimbursement_ratio")
@Api(tags = "药品报销比例管理")
public class Drug_reimbursement_ratioController {

    @Autowired
    private IDrug_reimbursement_ratioService drug_reimbursement_ratioService;

    // @ApiOperation("分页查询药品报销比例")
    // @PostMapping("/page")
    // public ResultVo<PageResultVO<Drug_reimbursement_ratioVO>> page(@RequestBody Drug_reimbursement_ratioQueryDTO queryDTO) {
    //     try {
    //         // 调用Service层方法进行复杂查询
    //         PageResultVO<Drug_reimbursement_ratioVO> pageVO = drug_reimbursement_ratioService.pageQuery(queryDTO);
    //         return ResultVo.ok(pageVO);
    //     } catch (Exception e) {
    //         return ResultVo.error(e);
    //     }
    // }


    @ApiOperation("查询全部药品报销比例")
    @GetMapping("/list")
    public ResultVo<List<Drug_reimbursement_ratioVO>> listAll(
            @ApiParam(value = "状态（启用/停用）", required = false) 
            @RequestParam(required = false) String status) {
        try {
            // 调用Service层方法查询所有报销比例
            List<Drug_reimbursement_ratioVO> list = drug_reimbursement_ratioService.listAll(status);
            return ResultVo.ok(list);
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }

    @ApiOperation("根据ID查询药品报销比例")
    @GetMapping("/{id}")
    public ResultVo<Drug_reimbursement_ratioVO> getById(@PathVariable("id") Long id) {
        try {
            // 简单查询直接使用MyBatis-Plus
            Drug_reimbursement_ratio ratio = drug_reimbursement_ratioService.getById(id);
            if (ratio != null) {
                Drug_reimbursement_ratioVO vo = new Drug_reimbursement_ratioVO();
                BeanUtils.copyProperties(ratio, vo);
                return ResultVo.ok(vo);
            } else {
                return ResultVo.fail("药品报销比例不存在");
            }
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }

    @ApiOperation("新增药品报销比例")
    @PostMapping("/add")
    public ResultVo<Boolean> add(@RequestBody Drug_reimbursement_ratioDTO ratioDTO) {
        try {
            // 简单的数据保存操作，直接使用MyBatis-Plus
            Drug_reimbursement_ratio ratio = new Drug_reimbursement_ratio();
            BeanUtils.copyProperties(ratioDTO, ratio);
            
            // 设置更新时间
            ratio.setUpdateTime(LocalDateTime.now());
            
            boolean result = drug_reimbursement_ratioService.save(ratio);
            if (result) {
                return ResultVo.ok(true, "新增药品报销比例成功");
            } else {
                return ResultVo.fail("新增药品报销比例失败");
            }
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }

    @ApiOperation("修改药品报销比例")
    @PutMapping("/update")
    public ResultVo<Boolean> update(@RequestBody Drug_reimbursement_ratioDTO ratioDTO) {
        try {
            // 判断ID是否存在
            if (ratioDTO.getRatioId() == null) {
                return ResultVo.fail("药品报销比例ID不能为空");
            }
            
            // 判断药品报销比例是否存在
            Drug_reimbursement_ratio existRatio = drug_reimbursement_ratioService.getById(ratioDTO.getRatioId());
            if (existRatio == null) {
                return ResultVo.fail("药品报销比例不存在");
            }
            
            // 简单的更新操作，直接使用MyBatis-Plus
            Drug_reimbursement_ratio ratio = new Drug_reimbursement_ratio();
            BeanUtils.copyProperties(ratioDTO, ratio);
            
            // 更新修改时间
            ratio.setUpdateTime(LocalDateTime.now());
            
            boolean result = drug_reimbursement_ratioService.updateById(ratio);
            if (result) {
                return ResultVo.ok(true, "修改药品报销比例成功");
            } else {
                return ResultVo.fail("修改药品报销比例失败");
            }
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }

    @ApiOperation("删除药品报销比例")
    @PostMapping("/delete/{id}")
    public ResultVo<Boolean> delete(@PathVariable("id") Long id) {
        try {
            // 判断药品报销比例是否存在
            Drug_reimbursement_ratio existRatio = drug_reimbursement_ratioService.getById(id);
            if (existRatio == null) {
                return ResultVo.fail("药品报销比例不存在");
            }
            
            // 简单的删除操作，直接使用MyBatis-Plus
            boolean result = drug_reimbursement_ratioService.removeById(id);
            if (result) {
                return ResultVo.ok(true, "删除药品报销比例成功");
            } else {
                return ResultVo.fail("删除药品报销比例失败");
            }
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }
    
    // @ApiOperation("批量删除药品报销比例")
    // @PostMapping("/batch/delete")
    // public ResultVo<Boolean> batchDelete(@RequestBody List<Long> ids) {
    //     try {
    //         if (ids == null || ids.isEmpty()) {
    //             return ResultVo.fail("请选择要删除的药品报销比例");
    //         }
            
    //         // 简单的批量删除操作，直接使用MyBatis-Plus
    //         boolean result = drug_reimbursement_ratioService.removeByIds(ids);
    //         if (result) {
    //             return ResultVo.ok(true, "批量删除药品报销比例成功");
    //         } else {
    //             return ResultVo.fail("批量删除药品报销比例失败");
    //         }
    //     } catch (Exception e) {
    //         return ResultVo.error(e);
    //     }
    // }
    
    
}

